package com.yylending.las.lending.increase;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.AESEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Pengrf on 2017/6/20.
 * Copyright  2017 www.yyfax.com. All Rights Reserved.
 */
public class IncreasePresenter extends Presenter<IncreaseModel, IncreasePresenter.View> {
	private final static String TAG = "IncreasePresenter";

	private String mRequestPage;
	private View mView;

	public static class Generator implements PresenterFactory<IncreasePresenter> {
		@Override
		public IncreasePresenter create() {
			return new IncreasePresenter();
		}
	}

	public interface View extends IView {
		void promoteByPolicySuccess();

		void promoteByPolicyFail(String msg);
	}

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new IncreaseModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void increaseLimitByPolicyResult(IncreaseModel.IncreaseResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				mView.promoteByPolicySuccess();
			} else {
				mView.promoteByPolicyFail(result.getRetMsg());
			}
		}
	}

	public void increaseLimitByPolicy(IncreaseModel.IncreaseParams increaseParams) {
		execute(new AESEntity(increaseParams), IncreaseModel.METHOD_INCREASE_BY_POLICY, true, null, mRequestPage);
	}
}
